<?php

namespace app\api\controller;

use app\api\model\Sign;
use app\common\controller\Api;
use fast\Http;
use think\Db;

use app\api\model\Joinpay;
use app\api\model\Leshuapay;
use app\api\model\Order;
use think\Exception;
use think\Request;

/**
 * 退款
 */
class Refund extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['index', 'test'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['test2'];

    // 在构造函数中应用中间件
    protected $middleware = [
        \app\api\middleware\AuthMiddleware::class
    ];

    public function index()
    {
        $params['ordersn'] = $this->request->param('ordersn');
        $params['hmac']    = $this->request->param('hmac');
        $params['nonce_str']    = $this->request->param('nonce_str');
        $params['price']    = $this->request->param('price');
        $params['RefundOrderNo']    = $this->request->param('RefundOrderNo');

        $sign = new Sign();

        $ret = $sign->check($params);

        if (!$ret){
            $this->error('退款失败，请检查签名');
        }


        $order = Db('orderLists')->where(['ordersn' => $params['ordersn']])->find();

        $refunds_param = [
            'merchant_no'       => $order['merchant_no'],
            'verifycode'       => $order['verifycode'],
            'productCode'       => $params['ordersn'],
            'p3_RefundOrderNo'  =>  $params['RefundOrderNo'],
            'p4_RefundAmount'   => $params['price'],
            'p5_RefundReason'   => '不想要了',
            'p6_NotifyUrl'      => 'https://baidu.com'
        ];

        $Joinpay = new Joinpay();

        $ret = $Joinpay->orderRedund($refunds_param);
        if ($ret){
            $this->success('退款成功', $this->request->param());
        }


        $this->error('退款失败,'.$ret['message']);
    }

    //重定向跳转重复支付
    public function test()
    {



    }



}
